###############################################################################
#                                                                             #
#           Skigin, Natán & Aníbal Pérez-Liñan                                #
#      "Preemptive Multipartism and Democratic Transitions"                   #
#                                                                             #
###############################################################################

#install.packages('sensemakr')
library(pacman)
p_load(sensemakr, readstata13, haven)

# Uncomment the following line and set your working directory
# setwd("")

# Choose dataset path
path <- "base_Skigin & Pérez-Liñán_PSRM_final.dta"
d <- read.dta13(path) # Read data
aut = subset(d, a_1==1 & legis11==1) # Subset of dictators with legislatures


#######################################
#               Figure A3             #               
#######################################

## GWF
m_GWF <- glm(gwf_democratization ~ frag + unrest + region + e_miinterc +
                         e_miinteco + pgdp + gpgdp + oilmin + popm + al_ethnic +
                         al_religion +  age_gwf +  age2_gwf + age3_gwf +
                         personal + enph, data = aut)
darfur.sensitivity_GWF <- sensemakr(model = m_GWF, 
                                treatment = "enph",
                                benchmark_covariates = "unrest",
                                kd = 7,
                                alpha = 0.05, 
                                reduce = TRUE)

# CGV
m_CGV <- glm(cgv_democratization ~ frag + unrest + region + e_miinterc +
           e_miinteco + pgdp + gpgdp + oilmin + popm + al_ethnic +
           al_religion +  age_gwf +  age2_gwf + age3_gwf +
           personal + enph, data = aut)
darfur.sensitivity_CGV <- sensemakr(model = m_CGV,
                                treatment = "enph",
                                benchmark_covariates = "unrest",
                                kd = 10,
                                alpha = 0.05, 
                                reduce = TRUE)

# Svolik
m_SV <- glm(sv_democratization ~ frag + unrest + region + e_miinterc +
           e_miinteco + pgdp + gpgdp + oilmin + popm + al_ethnic +
           al_religion +  age_gwf +  age2_gwf + age3_gwf +
           personal + enph, data = aut)
darfur.sensitivity_SV <- sensemakr(model = m_SV, 
                                treatment = "enph",
                                benchmark_covariates = "unrest",
                                kd = 5,
                                alpha = 0.05, 
                                reduce = TRUE)




png("SensitivityAll.png", res = 600, width = 7800, height = 2500)
par(mfrow = c(1, 3),              # 3 plots in a row
    mar = c(5, 4.5, 3, 1),        # margins: bottom, left, top, right
    oma = c(0, 0, 0, 0),          # no outer margins
    cex = .9,
    xpd = NA)                     # allow plotting outside plot region if needed
plot(darfur.sensitivity_GWF, sensitivity.of = "t-value", main = "GWF", cex.lab=1.2)
plot(darfur.sensitivity_CGV, sensitivity.of = "t-value", main = "CGV", cex.lab=1.2)
plot(darfur.sensitivity_SV, sensitivity.of = "t-value", main = "Svolik", cex.lab=1.2)
dev.off()

